package clearjerk.pic.control;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import clearjerk.pic.model.ImageMod;
import clearjerk.pic.view.GameFrame;

public class ImageMgr implements KeyListener{
	private ImageMod mod;
	public static void main(String[] args) throws InterruptedException{
		new ImageMgr();
	}
	public ImageMgr() throws InterruptedException{
		BufferedImage image = null;
		try {
		    // Read from a file
		    File file = new File("C:/Users/dcaryl/Downloads/penguins.jpg");
		    image = ImageIO.read(file);
		} catch (IOException e) {
		}
		if(image instanceof BufferedImage){
			mod = new ImageMod((BufferedImage)image);
		}
		int i = 1024;
		GameFrame frame = new GameFrame("", null);
		frame.getCanvas().addKeyListener(this);
	while(true){
		frame.setPixels(mod.pixelateImage(i));
		i/=2;
		Thread.sleep(1000);
	}
	}
	private static BufferedImage resize(BufferedImage img, int newW, int newH) {  
        int w = img.getWidth();  
        int h = img.getHeight();  
        BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);  
        Graphics2D g = dimg.createGraphics();  
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, newW, newH);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
        g.dispose();  
        return dimg;  
    }
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()){
		case KeyEvent.VK_X:
			System.exit(0);
			break;
		}
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}  
	}
